package com.ceba.test.modules.regist.logicService;

import abc.ID;
import cn.com.cqcca.util.FileUtil;
import com.ceba.base.utils.license.LicenseUtils;
import com.ceba.base.web.response.IResult;
import com.ceba.test.base.config.LogicPathUtil;
import com.ceba.test.base.enums.ResultStatusEnum;
import com.ceba.test.base.language.ResourceUtils;
import com.ceba.test.base.utils.StringUtil;
import com.ceba.test.modules.regist.entity.EnterpriseRegistLic;
import com.ceba.test.modules.regist.entity.LicRequestPar;
import com.ceba.test.modules.regist.entity.NewEnterprise;
import com.ceba.test.modules.regist.interfaces.LicBase;
import com.ceba.test.modules.regist.utils.LicJsonUtils;
import com.ceba.test.modules.storage.logicService.StorageFileCacheManage;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

import java.io.File;

public class DefaultLicImpl  implements LicBase {


    /**
     *解密base64 的 lic
     */
    @Override
    public IResult deCodeBase64Lic(LicRequestPar para) throws Exception {

        IResult iResult=new IResult(true);
        iResult.setResultCode(ResultStatusEnum.CG.getCode());
        String licCount ="";
        //获取客户的公司名 及 客户唯一编码
//        NewEnterprise  newEnterprise = new NewEnterprise();
        if(new File(para.getLicFilePath()).exists()){
            //将lic内容进行Base64解密
            licCount= LicenseUtils.readLicFileToDecode(para.getLicFilePath());
            if(!StringUtil.isEmptyString(licCount)){
                //将解密后的json字符串转换成对象
                EnterpriseRegistLic enterpriseRegistLic=  LicJsonUtils.jsonConvertObject(licCount);
                iResult.setData(enterpriseRegistLic);
            }else{
                iResult.setResultCode(ResultStatusEnum.SB.getCode());
                iResult.setResult(false);
                iResult.setMessage(ResourceUtils.getMessage("LIC_FILE_NON_EXISTENT"));
            }
        }else{
            iResult.setResultCode(ResultStatusEnum.SB.getCode());
            iResult.setResult(false);
            iResult.setMessage(ResourceUtils.getMessage("LIC_FILE_NON_EXISTENT"));
        }
        return iResult;
    }




    /**
     *通过base64 ASE 对lic  进行加密对包（对lic文件进行加密，并打成压缩包）
     */
    @Override
    public IResult enCodeBase64AndASELic(LicRequestPar para) throws Exception {
        IResult iResult=new IResult();
        iResult.setResultCode(ResultStatusEnum.CG.getCode());
        //对内容进行加密
        if(!StringUtil.isEmptyString(para.getStrCount())){
            //获取加密后的内容
          String newLicCount= LicenseUtils.encodeLicContent(para.getStrCount(),para.getEnterpriseId());
            File file = new File(para.getLicFilePath());
            //写入文件
            FileUtils.writeStringToFile(file,newLicCount,"utf-8");
            //文件写入成功  进行打包
            String licZipPath =   LicenseUtils.generatorLicFiles(para.getLicFilePath(),para.getEnterpriseId(),para.getLicPathDir());
//            iResult.setData(licZipPath);
            iResult.set("zipPath",licZipPath);
        }else{
            iResult.setResultCode(ResultStatusEnum.SB.getCode());
            iResult.setMessage(ResourceUtils.getMessage("LIC_FILE_COUNT_EXCEPTION"));
        }
        return iResult;
    }



    /**
     *解密base64 ASE 的lic
     */
    @Override
    public IResult deCodeBase64AndASELic(LicRequestPar para) throws Exception {
        IResult iResult=new IResult();
        iResult.setResultCode(ResultStatusEnum.CG.getCode());
        EnterpriseRegistLic  enterpriseRegistLic  = new EnterpriseRegistLic();
        //通过文件路径获取文件内容
         if(new File(para.getLicFilePath()).exists()){
             //读取文件内容
             String content =  FileUtils.readFileToString(new File(para.getLicFilePath()),"utf-8");
             if(!com.ceba.cebautils.StringUtil.isEmptyString(content)){
                 //对文件内容进行解码
               String newLicCount=  LicenseUtils.deCodeLicContent(content,para.getEnterpriseId());
               if(!StringUtil.isEmptyString(newLicCount)){
                   enterpriseRegistLic= LicJsonUtils.jsonConvertObject(newLicCount);
                   iResult.setData(enterpriseRegistLic);
               }else{
                   iResult.setResultCode(ResultStatusEnum.SB.getCode());
                   iResult.setMessage(ResourceUtils.getMessage("LIC_FILE_DECODE_COUNT_EXCEPTION"));
               }

             }else{
                 iResult.setResultCode(ResultStatusEnum.SB.getCode());
                 iResult.setMessage(ResourceUtils.getMessage("LIC_FILE_READE_COUNT_EXCEPTION"));
             }
         }else{
             iResult.setResultCode(ResultStatusEnum.SB.getCode());
             iResult.setMessage(ResourceUtils.getMessage("LIC_FILE_NON_EXISTENT"));
         }
        return iResult;
    }

}
